package coms.action;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import coms.datatransfer.ConferenceItemDTO;
//import coms.datatransfer.AssignedReviewDTO;
import coms.datatransfer.FileItemDTO;
import coms.datatransfer.ReviewDTO;
import coms.datatransfer.UserDTO;
import coms.ejb.sessionbean.ConferenceItemBeanRemote;
import coms.ejb.sessionbean.FileBean;
import coms.ejb.sessionbean.FileBeanRemote;
import coms.ejb.sessionbean.ReviewBeanRemote;
import coms.ejb.sessionbean.UserBeanRemote;
import coms.service.BusinessDelegate;

import java.io.File;
import java.util.*;

public class FileItemAction extends ActionSupport implements ServletRequestAware{
	
	private static final long serialVersionUID = 1L;
	
	private ArrayList<FileItemDTO> fileItemList = new ArrayList<FileItemDTO>();
	private FileItemDTO fileItem;
	private int extraReviewers=0;
	private ArrayList<String> fileReviewers = new ArrayList<String>();
	
	private File userImage;
	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	private String userImageContentType;
	private String userImageFileName;

	private HttpServletRequest servletRequest;
	

	public ArrayList<String> getFileReviewers() {
		return fileReviewers;
	}

	public void setFileReviewers(ArrayList<String> fileReviewers) {
		this.fileReviewers = fileReviewers;
	}

	public int getExtraReviewers() {
		return extraReviewers;
	}

	public void setExtraReviewers(int extraReviewers) {
		this.extraReviewers = extraReviewers;
	}

	public FileItemDTO getFileItem() {
		return fileItem;
	}

	public void setFileItem(FileItemDTO fileItem) {
		this.fileItem = fileItem;
	}

	public ArrayList<FileItemDTO> getFileItemList() {
		return fileItemList;
	}

	public void setFileItemList(ArrayList<FileItemDTO> fileItemList) {
		this.fileItemList = fileItemList;
	}

	private BusinessDelegate delegate;
	private FileBeanRemote bean;
	private UserBeanRemote userBean;
	private ArrayList<String> reviewersIdList = new ArrayList<String>();
	private ArrayList<String> reviewerList = new ArrayList<String>();
	
	

	
	public ArrayList<String> getReviewerList() {
		return reviewerList;
	}

	public void setReviewerList(ArrayList<String> reviewerList) {
		this.reviewerList = reviewerList;
	}

	public ArrayList<String> getReviewersIdList() {
		return reviewersIdList;
	}

	public void setReviewersIdList(ArrayList<String> reviewersIdList) {
		this.reviewersIdList = reviewersIdList;
	}

	public FileItemAction(){
		delegate = new BusinessDelegate();
		bean = (FileBeanRemote)delegate.getBean("fileBean");
		userBean = (UserBeanRemote)delegate.getBean("userBean");
	}
    
	public String execute() throws Exception {
		
		return "success";
	}
	
	public String assignReview() {
		try{
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		ArrayList<UserDTO> reviewersNewList = new ArrayList<UserDTO>();
    	for (String reviewer: fileReviewers)
    	{
    		
    		if((reviewer != null) && (reviewer != ""))
    		{
    			UserDTO user = new UserDTO();
    			user.setUserID(Integer.parseInt((reviewer)));
    			UserDTO retrievedUser = userBean.validateUser(user);
    			reviewersNewList.add(retrievedUser);
    		}
    	}
    	if(session.get("fileID") != null)
    		bean.assignReviewers(reviewersNewList,Integer.parseInt((String)session.get("fileID")));
    	
		return "success";
		} catch (Exception e){
			return "exception";
		}
	}
	
	public String displayFiles() throws Exception {
		
		
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		
		if(request.getParameter("conferenceItemID") != null)
		session.put("conferenceItemId", request.getParameter("conferenceItemID"));
		
		if(session.get("conferenceItemId") != null)
		fileItemList = (ArrayList<FileItemDTO>)bean.
				        findFileItemByConfItem(Integer.parseInt((String)session.get("conferenceItemId")));
		return "display";
	}
	
	
	public String displayReviewersList() throws Exception {
		
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		Map<String,Object> session = ActionContext.getContext().getSession();
	    
		
		if(request.getParameter("fileID") != null)
			session.put("fileID", request.getParameter("fileID"));
			
		if(session.get("fileID") != null)
			fileItem = (FileItemDTO)bean.findFileItemById(Integer.parseInt((String)session.get("fileID")));
		
		for(UserDTO e: fileItem.getListOfReviewers())
		{
			reviewerList.add(Integer.toString(e.getUserID()));
		}
		
			ArrayList<UserDTO> reviewersList = (ArrayList<UserDTO>)userBean.findUserByRoleID(2);
			System.out.println("file: " + reviewersList.get(0));
			
			for (UserDTO r:reviewersList)
			 reviewersIdList.add(Integer.toString(r.getUserID()));	
		      
			
			 extraReviewers = fileItem.getConferenceItemDTO().getMaxReviewers() - reviewersList.size();
		    
		   
		return "success";
	}
	
	public String uploadFile()
	{
		
		try
		{
			String filePath = servletRequest.getSession().getServletContext().getRealPath("/");//"D:/Semester 2 Projects/COMS/";//servletRequest.getSession().getServletContext().getRealPath("/");
		   System.out.println("Server path:" + filePath);
		   System.out.println(this.userImageFileName);
		   File fileToCreate = new File(filePath, this.userImageFileName);

		   FileUtils.copyFile(this.userImage, fileToCreate);
		
		}
		
		catch(Exception e){
			
			 e.printStackTrace();
			   addActionError(e.getMessage());
			
		}
		   return "SUCCESS";
	}
	
	
	public String backuploadFile(){
		
		return "SUCCESS";
		
	}
	
	 
	
	
}

